package com.cn.customer.housekeeping.housekeepingc.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cn.customer.housekeeping.housekeepingc.R;
import com.cn.customer.housekeeping.housekeepingc.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingc.bean.ServicekindListResp;
import com.cn.customer.housekeeping.housekeepingc.bean.ServicekindResp;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.InterfacePresenter;
import com.cn.customer.housekeeping.housekeepingc.bridge.BridgeFactory;
import com.cn.customer.housekeeping.housekeepingc.bridge.Bridges;
import com.cn.customer.housekeeping.housekeepingc.bridge.cache.sharePref.EBSharedPrefManager;
import com.cn.customer.housekeeping.housekeepingc.bridge.cache.sharePref.EBSharedPrefUser;
import com.cn.customer.housekeeping.housekeepingc.constant.Constants;
import com.cn.customer.housekeeping.housekeepingc.constant.URLUtil;
import com.cn.customer.housekeeping.housekeepingc.ui.base.QuickAdapter;
import com.cn.customer.housekeeping.housekeepingc.ui.helper.ItemHelper;
import com.cn.customer.housekeeping.housekeepingc.util.NavUtils;
import com.cn.customer.housekeeping.housekeepingc.util.ToastUtil;
import com.google.gson.Gson;
import com.lfq.pulltorefresh.library.PullToRefreshBase;
import com.lfq.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;


public class OrderListActivity extends Fragment implements IUserLoginView {
    private InterfacePresenter mInterfacePresenter;
    private EBSharedPrefManager manager = BridgeFactory.getBridge(Bridges.SHARED_PREFERENCE);
    private ListView mListView;
    private PullToRefreshListView mPullToRefreshListView;
    private int pageNum = 1;
    private int pageSize = 20;
    private String subtypes, type, cityname;
    private ServicekindResp mServicekindResp;


    public static OrderListActivity newInstance(String type, String subtype, String cityname) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.typezi, type);
        bundle.putString(Constants.subtypezi, subtype);
        bundle.putString(Constants.cityname, cityname);
        OrderListActivity fragment = new OrderListActivity();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View contextView = inflater.inflate(R.layout.activity_main_list, container, false);
        mInterfacePresenter = new InterfacePresenter();
        mInterfacePresenter.attachView(this);
        mPullToRefreshListView = (PullToRefreshListView) contextView.findViewById(R.id.pull_refresh_list);
        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.DISABLED);
        mListView = mPullToRefreshListView.getRefreshableView();
        mListView.setHeaderDividersEnabled(false);
        mListView.setDividerHeight(0);
        initListeners();
        initData();
        return contextView;
    }

    public void initListeners() {

        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum = 1;
//                mInterfacePresenter.getBlacklist(pageNum, pageSize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//                mInterfacePresenter.getBlacklist(pageNum, pageSize);
            }
        });

        mPullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int headerCount = mListView.getHeaderViewsCount();
                if (position >= headerCount) {
                     //订单详情
                    NavUtils.gotoOrderDetailsActivity(getActivity(),    mServicekindResp.getServiceList().get(position).getServiceid());
                }
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private QuickAdapter<ServicekindListResp> mAdapter;

    public void initData() {
        type = getArguments().getString(Constants.typezi);
        cityname = getArguments().getString(Constants.cityname);
        subtypes = getArguments().getString(Constants.subtypezi);

        ItemHelper mItemHelper = new ItemHelper();
        mAdapter = mItemHelper.setAdapterOrderList(getActivity(), new ArrayList());
        mListView.setAdapter(mAdapter);
        mInterfacePresenter.getServicelist(type, subtypes, pageNum + "", pageSize + "", manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.lat, ""), manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.lon, ""), cityname);
    }


    public void onClick(View v) {
    }

    @Override
    public void onError(BaseResp s) {
        mPullToRefreshListView.onRefreshComplete();
    }

    @Override
    public void onSuccess(BaseResp s) {
        mPullToRefreshListView.onRefreshComplete();
        if (s.getUrl().equals(URLUtil.url_get_getservicelist)) {//列表
            Gson gson = new Gson();
            mServicekindResp = gson.fromJson(s.getData(), ServicekindResp.class);
            if (mServicekindResp != null && mServicekindResp.getServiceList().size() > 0) {

                mAdapter.replaceAll(mServicekindResp.getServiceList());

                pageNum++;
            } else {
                if (pageNum == 1) {
                    ToastUtil.makeText(getActivity(), "暂无数据");
                } else {
                    ToastUtil.makeText(getActivity(), "没有更多数据");
                }
            }

        }


    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }


}